﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class AnalysGroupBriefViewModel : NodeViewModel
    {
        #region Properties

        private IList<AnalysGroupBrief> analysGroup;
        public IList<AnalysGroupBrief> AnalysGroup
        {
            get { return analysGroup; }
            set
            {
                analysGroup = value;
                NotifyChanged("AnalysGroup");
            }
        }

        private DateTime _dpBegin;

        public DateTime dpBegin
        {
            get { return _dpBegin; }
            set
            {
                _dpBegin = value;
                NotifyChanged("dpBegin");
            }
        }

        private DateTime _dpEnd;

        public DateTime dpEnd
        {
            get { return _dpEnd; }
            set
            {
                _dpEnd = value;
                NotifyChanged("dpEnd");
            }
        }

        private AnalysGroupBrief _selectedAnalysGB;
        public AnalysGroupBrief SelectedAnalysGB
        {
            get { return _selectedAnalysGB; }
            set
            {
                _selectedAnalysGB = value;
                NotifyChanged("SelectedAnalysGB");
                LoadBucket(_selectedAnalysGB);
            }
        }

        private BucketBrief _bucketAuto;
        public BucketBrief BucketAuto
        {
            get { return _bucketAuto; }
            set
            {
                _bucketAuto = value;
                NotifyChanged("BucketAuto");
            }
        }

        private BucketBrief _bucketManual;
        public BucketBrief BucketManual
        {
            get { return _bucketManual; }
            set
            {
                _bucketManual = value;
                NotifyChanged("BucketManual");
            }
        }

        private int _selectedAnalysGBIndex;
        public int SelectedAnalysGBIndex
        {
            get { return _selectedAnalysGBIndex; }
            set
            {
                _selectedAnalysGBIndex = value;
                NotifyChanged("SelectedAnalysGBIndex");
            }
        }

        private int _selectedTrainIndex;
        public int SelectedTrainIndex
        {
            get { return _selectedTrainIndex; }
            set
            {
                _selectedTrainIndex = value;
                NotifyChanged("SelectedTrainIndex");
                ChangeTrain(_selectedTrainIndex);
            }
        }
        #endregion


        public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
        public SampleCommon sc = new SampleCommon();


        public AnalysGroupBriefViewModel()
        {
            dpBegin = DateTime.Now.AddDays(-20);
            dpEnd = DateTime.Now;
            Init();
        }

        public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/sample.png", UriKind.RelativeOrAbsolute);
        }

        public AnalysGroupBriefViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "AnalysGroupBriefViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            dpBegin = DateTime.Now.AddDays(-20);
            dpEnd = DateTime.Now;
            Init();
        }

        #region Commands
        private void Init()
        {
            this.AnalysGroup = GetAnalysGroup();
        }

        public IList<AnalysGroupBrief> GetAnalysGroup(DateTime bDate, DateTime eDate)
        {
            return ConvertData(sp.GetAnalysGroupBriefWithDate(bDate, eDate)).Where(o => o.IsTrain == "汽车").ToList();
        }

        public IList<AnalysGroupBrief> GetAnalysGroup()
        {
            var agb = sp.GetAnalysGroupBriefToday();
            if (agb.Count() > 0)
                return ConvertData(agb).Where(o => o.IsTrain == "汽车").ToList();
            else return null;
        }

        public IList<AnalysGroupBrief> ConvertData(IEnumerable<AnalysGroupInfo> agInfo)
        {
            analysGroup = new List<AnalysGroupBrief>();
            if (agInfo != null)
            {
                foreach (var agInfoItem in agInfo)
                {
                    var agb = new AnalysGroupBrief();
                    agb.ID = agInfoItem.ID;
                    agb.AnalysBatchID = agInfoItem.质检凭证号;
                    agb.Status = sc.DicSampleStatus[agInfoItem.检验状态];
                    agb.Materiel = agInfoItem.物料.Name;
                    agb.CoalOrigin = agInfoItem.CoalOrigin != null ? agInfoItem.CoalOrigin.Name : string.Empty;
                    var list = sp.GetBriefByGroupID(agInfoItem.ID);
                    agb.TotalBucketSample = list[0];
                    agb.CompleteBucketSample = list[1];
                    agb.TotalAnalysCommission = list[2];
                    agb.CompleteAnalysCommission = list[3];
                    switch (list[4])
                    {
                        case 0:
                            agb.IsTrain = "汽车";
                            break;
                        case 1:
                            agb.IsTrain = "火车";
                            break;
                        default:
                            agb.IsTrain = null;
                            break;
                    }

                    var po = new PurchaseOrder();
                    if (null != sp.GetPOByGroupID(agInfoItem.ID))
                    {
                        var ipo = sp.GetPOByGroupID(agInfoItem.ID);
                        //var isupplier = sp.GetSupplierByPurchaseID(ipo.ID);

                        po.ApprovalMark = ipo.ApprovalMark;
                        po.Contacts = ipo.Supplier.Contacts;
                        po.Creater = ipo.Creater;
                        po.CreateTime = ipo.CreateTime.ToShortDateString();
                        po.Factory = ipo.Factory;
                        po.ID = ipo.ID;
                        po.ManualMark = ipo.ManualMark;
                        po.Modifier = ipo.Modifier;
                        po.ModifyTime = ipo.ModifyTime != null ? ipo.ModifyTime.Value.ToShortDateString() : string.Empty;
                        po.OrderStatus = ipo.OrderStatus;
                        po.OrderType = ipo.OrderType;
                        po.PhoneNo = ipo.Supplier.PhoneNo;
                        po.PricingProgress = ipo.PricingProgress;
                        po.ProjectNo = ipo.ProjectNo;
                        po.PurchaseContact = ipo.PurchaseContact;
                        po.PurchaseGroup = ipo.PurchaseGroup;
                        po.PurchaseMethod = ipo.PurchaseMethod;
                        po.PurchaseOrderID = ipo.PurchaseOrderID;
                        po.PurchaseOrganization = ipo.PurchaseOrganization;
                        po.SupplierName = ipo.Supplier.Name;
                        po.SupplierNo = ipo.Supplier.SupplierNo;
                        po.ValidEnd = ipo.ValidEnd.ToShortDateString();
                        po.ValidStart = ipo.ValidStart.ToShortDateString();

                        agb.PurOr = po;
                    }

                    var bs = sp.GetBucketByGroupID(agInfoItem.ID);

                    if (bs != null)
                    {
                        var bbList = new List<BucketBrief>();
                        foreach (var bsi in bs)
                        {
                            var bb = new BucketBrief();
                            var ac = sp.GetAnalysCommissionsByBucketId(bsi.ID);

                            if (ac != null)
                            {
                                var acbList = new List<AnalysCommisBrief>();
                                foreach (var aci in ac)
                                {
                                    var acb = new AnalysCommisBrief();
                                    acb.VehicleID = aci.License;
                                    acb.DeliverStatus = sc.DicDiliverStatus[aci.DeliverStatus];
                                    acb.SampleTime = sp.GetSampleRecordByACId(aci.ID)[0];
                                    acb.SamplePoint = sp.GetSampleRecordByACId(aci.ID)[1];
                                    acbList.Add(acb);
                                }
                                bb.AnalysCommisBriefs = acbList;
                                bb.AnalysCommissionCount = bb.AnalysCommisBriefs.Count();
                            }
                            bb.BID = bsi.ID;
                            bb.BucketID = bsi.样桶号;

                            bb.SampleMachineID = sc.DicSampleMachine[bsi.取样设备号] ;
                            bb.SampleStatus = sc.DicBucketStatus[bsi.大样状态];
                            bbList.Add(bb);
                        }
                        agb.BucketBriefs = bbList;
                    }
                   
                    analysGroup.Add(agb);
                }
            }
            return analysGroup;
        }


        public ICommand Query
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.AnalysGroup = GetAnalysGroup(dpBegin, dpEnd);
                });
            }
        }

        public void LoadBucket(AnalysGroupBrief selectedAG)
        {
            BucketAuto = new BucketBrief();
            BucketManual = new BucketBrief();

            if (selectedAG != null)
            {
                foreach (var bi in selectedAG.BucketBriefs)
                {
                    if (bi.SampleMachineID == "人工取样")
                    {
                        BucketManual = bi;
                    }
                    else
                        BucketAuto = bi;
                }
            }
        }

        public void ChangeTrain(int trainIndex)
        {
            switch (trainIndex)
            {
                case 1:
                    this.AnalysGroup = ConvertData(sp.GetAnalysGroupBriefWithDate(dpBegin, dpEnd)).Where(o => o.IsTrain == "火车").ToList();
                    break;
                case 2:
                    this.AnalysGroup = ConvertData(sp.GetAnalysGroupBriefWithDate(dpBegin, dpEnd)).Where(o => o.IsTrain == "汽车").ToList();
                    break;
                default:
                    this.AnalysGroup = ConvertData(sp.GetAnalysGroupBriefWithDate(dpBegin, dpEnd));
                    break;
            }

            
        }

        #endregion
    }

    public class PurchaseOrder
    {
        public PurchaseOrder() { }

        public Guid ID { get; set; }

        [DisplayName("采购订单号")]
        public string PurchaseOrderID { get; set; }

        [DisplayName("项目号")]
        public string ProjectNo { get; set; }

        //供应商编号
        //public string SupplierID { get; set; }

        [DisplayName("供应商编号")]
        public string SupplierNo { get; set; }

        [DisplayName("供应商名称")]
        public string SupplierName { get; set; }

        [DisplayName("联系人")]
        public string Contacts { get; set; }

        [DisplayName("电话")]
        public string PhoneNo { get; set; }

        [DisplayName("批准标识")]
        public string ApprovalMark { get; set; }

        [DisplayName("定价过程")]
        public string PricingProgress { get; set; }

        [DisplayName("订单类型")]
        public string OrderType { get; set; }

        [DisplayName("采购组织")]
        public string PurchaseOrganization { get; set; }

        [DisplayName("采购方法名称")]
        public string PurchaseMethod { get; set; }

        [DisplayName("工厂")]
        public string Factory { get; set; }

        [DisplayName("采购组")]
        public string PurchaseGroup { get; set; }

        [DisplayName("有效期开始")]
        public string ValidStart { get; set; }

        [DisplayName("有效期结束")]
        public string ValidEnd { get; set; }

        [DisplayName("采购合同")]
        public string PurchaseContact { get; set; }

        [DisplayName("手工标记")]
        public string ManualMark { get; set; }

        [DisplayName("创建人")]
        public string Creater { get; set; }

        [DisplayName("创建时间")]
        public string CreateTime { get; set; }

        [DisplayName("修改人")]
        public string Modifier { get; set; }

        [DisplayName("修改时间")]
        public string ModifyTime { get; set; }

        [DisplayName("状态")]
        public string OrderStatus { get; set; }
    }

    /// <summary>
    /// 组批
    /// </summary>
    public class AnalysGroupBrief
    {
        public AnalysGroupBrief() { }

        
        public Guid ID { get; set; }
        [DisplayName("质检凭证号")]
        public string AnalysBatchID { get; set; }

        [DisplayName("是否火车")]
        public string IsTrain { get; set; }
         [DisplayName("状态")]
        public string Status { get; set; }

        [DisplayName("物料")]
         public string Materiel { get; set; }

        [DisplayName("大样数")]
        public int? TotalBucketSample { get; set; }

        [DisplayName("大样完成数")]
        public int? CompleteBucketSample { get; set; }

        [DisplayName("车辆数")]
        public int? TotalAnalysCommission { get; set; }

        [DisplayName("车辆完成数")]
        public int? CompleteAnalysCommission { get; set; }

        [DisplayName("煤源地")]
        public string CoalOrigin { get; set; }

        public PurchaseOrder PurOr { get; set; }

        public List<BucketBrief> BucketBriefs { get; set; }
    }

    /// <summary>
    /// 大样
    /// </summary>
    public class BucketBrief
    {
        public BucketBrief() { }

        public Guid BID { get; set; }

        [DisplayName( "样桶号")]
        public string BucketID { get; set; }

        [DisplayName("取样设备号")]
        //0=人工取样，1=1#火车取样平台 ……
        public string SampleMachineID { get; set; }

        [DisplayName("大样状态")]
        public string SampleStatus { get; set; }

        public int AnalysCommissionCount { get; set; }

        public List<AnalysCommisBrief> AnalysCommisBriefs { get; set; }
    }

    /// <summary>
    /// 取样委托
    /// </summary>
    public class AnalysCommisBrief
    {
        public AnalysCommisBrief() { }

        public string VehicleID { get; set; }

        public string DeliverStatus { get; set; }

        public string SampleTime { get; set; }

        public string SamplePoint { get; set; }
    }


    public class ChangeBucketStatus : IValueConverter
    {
        public SampleCommon sc = new SampleCommon();
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int key = (int)value;
            string strValue = sc.DicBucketStatus[key];

            return strValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
